<template>
  <div class="register-bg">
    <div class="register-container">
      <h2>用户注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input v-model="username" id="username" type="text" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input v-model="password" id="password" type="password" placeholder="请输入密码" required />
        </div>
        <div class="form-group">
          <label>身份</label>
          <div class="role-group">
            <label><input type="radio" value="3" v-model="roleId" required /> 学生</label>
            <label><input type="radio" value="2" v-model="roleId" required /> 教师</label>
          </div>
        </div>
        <div class="form-group">
          <label for="email">邮箱</label>
          <input v-model="email" id="email" type="email" placeholder="邮箱" readonly />
        </div>
        <button type="submit" :disabled="loading">{{ loading ? '注册中...' : '注册' }}</button>
        <div v-if="error" class="error">{{ error }}</div>
      </form>
    </div>
  </div>
</template>

<script>
import axios from '@/utils/request'

export default {
  name: 'RegisterForm',
  data() {
    return {
      username: '',
      password: '',
      roleId: '',
      loading: false,
      error: '',
      email: '',
      registerToken: ''
    }
  },
  created() {
    // 从查询参数获取邮箱
    const urlParams = new URLSearchParams(window.location.search);
    this.email = urlParams.get('email') || '';
    
    // 从localStorage获取注册令牌
    this.registerToken = localStorage.getItem('tempRegisterToken') || '';
    
    // 如果没有邮箱或注册令牌，返回验证页面
    if (!this.email || !this.registerToken) {
      // 清理localStorage
      localStorage.removeItem('tempRegisterToken');
      this.$router.replace({ name: 'register-verify' })
    }
    
    // 使用完后清理localStorage中的临时令牌
    localStorage.removeItem('tempRegisterToken');
  },
  methods: {
    async handleRegister() {
      this.loading = true
      this.error = ''
      try {
        await axios.post('/auth/register', {
          username: this.username,
          password: this.password,
          email: this.email,
          roleId: this.roleId
        }, {
          headers: {
            'X-Register-Token': this.registerToken
          }
        })
        // 注册成功跳转登录
        this.$router.push({ name: 'login' })
      } catch (e) {
        this.error = e.response?.data?.message || '注册失败'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-container {
  width: 100%;
  max-width: 400px;
  padding: 40px 30px;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.register-container h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 28px;
}

.form-group {
  margin-bottom: 20px;
  width: 100%;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 16px;
}

input[type="text"],
input[type="password"],
input[type="email"] {
  width: 100%;
  padding: 14px 15px;
  border: 2px solid #e1e5ee;
  border-radius: 10px;
  font-size: 16px;
  background: #f8f9fa;
  transition: all 0.3s ease;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="email"]:focus {
  border-color: #409eff;
  outline: none;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.2);
}

input[readonly] {
  background-color: #f0f0f0;
  cursor: not-allowed;
}

.role-group {
  display: flex;
  gap: 24px;
  margin-top: 4px;
}

button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 17px;
  font-weight: bold;
  letter-spacing: 1px;
  margin-top: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.error {
  color: #f56c6c;
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
  padding: 10px;
  border-radius: 5px;
  background: #fef0f0;
}
</style>